
package ch.hearc.meteo.imp.use.remote;

import java.io.FileInputStream;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.rmi.RemoteException;
import java.util.Properties;

import javax.swing.JOptionPane;

import ch.hearc.meteo.imp.afficheur.real.JDialogPreferencesLocal;
import ch.hearc.meteo.spec.afficheur.AffichageOptions;
import ch.hearc.meteo.spec.meteo.MeteoServiceOptions;
import ch.hearc.meteo.spec.meteo.MeteoService_I;
import ch.hearc.meteo.spec.meteo.exception.MeteoServiceException;
import ch.hearc.meteo.spec.meteo.listener.MeteoListener_I;
import ch.hearc.meteo.spec.meteo.listener.event.MeteoEvent;
import ch.hearc.meteo.spec.reseau.AfficheurManager_I;
import ch.hearc.meteo.spec.reseau.AfficheurServiceWrapper_I;
import ch.hearc.meteo.spec.reseau.MeteoServiceWrapper;

import com.bilat.tools.reseau.rmi.IdTools;
import com.bilat.tools.reseau.rmi.RmiTools;
import com.bilat.tools.reseau.rmi.RmiURL;

public class PCLocal implements PC_I
	{

	/*------------------------------------------------------------------*\
	|*							Constructeurs							*|
	\*------------------------------------------------------------------*/

	public PCLocal(MeteoService_I meteoService, MeteoServiceOptions meteoServiceOptions, String portCom, AffichageOptions affichageOptions, RmiURL rmiURLafficheurManager)
		{
		this.meteoService = meteoService;
		this.affichageOptions = affichageOptions;
		this.rmiURLafficheurManager = rmiURLafficheurManager;

		meteoServiceWrapper = new MeteoServiceWrapper(meteoService);

		int rmiPort = 0;
		Properties p = new Properties();
		try
			{
			p.load(new FileInputStream(JDialogPreferencesLocal.PROPERTY_FILENAME));
			rmiPort = Integer.valueOf(p.getProperty(JDialogPreferencesLocal.SERVER_PORT_PROPERTY_NAME));
			}
		catch (Exception e)
			{
			// TODO Auto-generated catch block
			e.printStackTrace();
			}

		try
			{
			rmiURL = new RmiURL(RMI_ID, InetAddress.getLocalHost(), rmiPort);
			}
		catch (UnknownHostException e)
			{
			// TODO Auto-generated catch block
			e.printStackTrace();
			}
		}

	/*------------------------------------------------------------------*\
	|*							Methodes Public							*|
	\*------------------------------------------------------------------*/

	@Override
	public void run()
		{
		try
			{
			server(); // avant
			}
		catch (Exception e)
			{
			System.err.println("[PCLocal :  run : server : failed");
			e.printStackTrace();
			}

		try
			{
			client(); // apr�s
			}
		catch (RemoteException e)
			{
			System.err.println("[PCLocal :  run : client : failed");
			e.printStackTrace();
			}
		}

	/*------------------------------*\
	|*				Get				*|
	\*------------------------------*/

	public MeteoServiceWrapper getMeteoServiceWrapper()
		{
		return this.meteoServiceWrapper;
		}

	public MeteoService_I getMeteoService()
		{
		return meteoService;
		}

	public AfficheurServiceWrapper_I getAfficheurService()
		{
		return this.afficheurServiceWrapper;
		}

	/*------------------------------------------------------------------*\
	|*							Methodes Private						*|
	\*------------------------------------------------------------------*/


	/*------------------------------*\
	|*				server			*|
	\*------------------------------*/

	private void server() throws MeteoServiceException, RemoteException
		{
		RmiTools.shareObject(meteoServiceWrapper, rmiURL);
		}

	/*------------------------------*\
	|*				client			*|
	\*------------------------------*/

	private void client() throws RemoteException
		{
		AfficheurManager_I afficheurManager = (AfficheurManager_I)RmiTools.connectionRemoteObjectBloquant(rmiURLafficheurManager);
		final RmiURL rmiURLafficherService = afficheurManager.createRemoteAfficheurService(affichageOptions, rmiURL);
		afficheurServiceWrapper = (AfficheurServiceWrapper_I)RmiTools.connectionRemoteObjectBloquant(rmiURLafficherService);
		meteoService.addMeteoListener(new MeteoListener_I()
			{

				@Override
				public void temperaturePerformed(MeteoEvent event)
					{
					try
						{
						afficheurServiceWrapper.printTemperature(event);
						}
					catch (RemoteException e)
						{
						// TODO Auto-generated catch block
						e.printStackTrace();
						}
					}

				@Override
				public void pressionPerformed(MeteoEvent event)
					{
					try
						{
						afficheurServiceWrapper.printPression(event);
						}
					catch (RemoteException e)
						{
						// TODO Auto-generated catch block
						e.printStackTrace();
						}
					}

				@Override
				public void altitudePerformed(MeteoEvent event)
					{
					try
						{
						afficheurServiceWrapper.printAltitude(event);
						}
					catch (RemoteException e)
						{
						// TODO Auto-generated catch block
						e.printStackTrace();
						}
					}
			});

		Thread thread = new Thread(new Runnable()
			{

				@Override
				public void run()
					{
					while(true)
						{
						try
							{
							afficheurServiceWrapper.remoteTest();
							}
						catch (Exception e)
							{
							JOptionPane.showMessageDialog(null, "Le PC Central ne répond plus", "Erreur", JOptionPane.ERROR_MESSAGE);
							System.exit(-1);
							}

						try
							{
							Thread.sleep(RMI_LOOKUP_SLEEP_MS);
							}
						catch (InterruptedException e)
							{
							// TODO Auto-generated catch block
							e.printStackTrace();
							}
						}

					}
			});

			thread.start();

		}

	/*------------------------------------------------------------------*\
	|*							Attributs Private						*|
	\*------------------------------------------------------------------*/

	// Input
	private AffichageOptions affichageOptions;
	private RmiURL rmiURLafficheurManager;
	private MeteoService_I meteoService;
	private RmiURL rmiURL;

	// Tools
	private MeteoServiceWrapper meteoServiceWrapper;
	private AfficheurServiceWrapper_I afficheurServiceWrapper;

	// Tools final
	private static final String PREFIXE = "METEO_SERVICE_WRAPPER";
	public static final String RMI_ID = IdTools.createID(PREFIXE);
	private static final int RMI_LOOKUP_SLEEP_MS = 500;
	}
